package com.example.assetmanagement.controller;

import com.example.assetmanagement.common.ApiResponse;
import com.example.assetmanagement.common.PageRequest;
import com.example.assetmanagement.common.PageResponse;
import com.example.assetmanagement.domain.Category;
import com.example.assetmanagement.service.CategoryService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@RequiredArgsConstructor
public class CategoryController {

    private final CategoryService categoryService;

    @GetMapping("/list")
    public ApiResponse<PageResponse<Category>> list(@Valid PageRequest pageRequest,
                                                    @RequestParam(value = "name", required = false) String name) {
        return ApiResponse.success(categoryService.list(pageRequest, name));
    }

    @GetMapping("/tree")
    public ApiResponse<List<Category>> tree() {
        return ApiResponse.success(categoryService.tree());
    }

    @GetMapping("/all")
    public ApiResponse<List<Category>> all() {
        return ApiResponse.success(categoryService.listAll());
    }

    @GetMapping("/{id}")
    public ApiResponse<Category> detail(@PathVariable Long id) {
        return ApiResponse.success(categoryService.getById(id));
    }

    @PostMapping
    public ApiResponse<?> add(@RequestBody @Valid Category category) {
        categoryService.add(category);
        return ApiResponse.success(null);
    }

    @PutMapping("/{id}")
    public ApiResponse<?> update(@PathVariable Long id, @RequestBody @Valid Category category) {
        categoryService.update(id, category);
        return ApiResponse.success(null);
    }

    @DeleteMapping("/{id}")
    public ApiResponse<?> delete(@PathVariable Long id) {
        categoryService.delete(id);
        return ApiResponse.success(null);
    }

    @DeleteMapping("/batch")
    public ApiResponse<?> batchDelete(@RequestBody List<Long> ids) {
        categoryService.batchDelete(ids);
        return ApiResponse.success(null);
    }
} 